package com.emptyMe.pet.service.imp;

import com.emptyMe.pet.domain.MasterMsg;
import com.emptyMe.pet.domain.Pet;
import com.emptyMe.pet.mapper.PetMapper;
import com.emptyMe.pet.query.PetQuery;
import com.emptyMe.pet.service.IPetService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PetServiceImpl implements IPetService {
    @Autowired
    private PetMapper mapper;
    @Override
    public void publishMessage(MasterMsg mm) {
        mapper.saveMasterMsg(mm);
    }

    @Override
    public void save(Pet pet) {
        mapper.save(pet);
    }

    @Override
    public void update(Pet pet) {
        mapper.update(pet);
    }

    @Override
    public void remove(Long id) {
        mapper.delById(id);
    }

    @Override
    public void removeMany(List<Pet> pets) {
        for (Pet pet : pets) {
            mapper.delById(pet.getId());
        }

    }

    @Override
    public Pet getById(Long id) {
        return mapper.findById(id);
    }

    @Override
    public PageInfo<Pet> searchData(PetQuery query) {
        PageHelper.startPage(query.getCurrentPage(), query.getPageSize());
        List<Pet> list = mapper.searchData(query);
        Page<Pet> page = (Page<Pet>) list;
        return page.toPageInfo();
    }
}
